package com.doge.calcalbum.db

import androidx.lifecycle.LiveData
import androidx.room.*
import com.doge.calcalbum.db.model.Entry

@Dao
interface EntryDao {
    @Query("SELECT * FROM entry WHERE (albumId = :albumId AND deleteTime <= 0) ORDER BY createTime LIMIT 1")
    fun queryOne(albumId: Int): Entry?

    @Query("SELECT * FROM entry WHERE (albumId = :albumId AND deleteTime <= 0)")
    fun queryLiveData(albumId: Int): LiveData<List<Entry>>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insert(entry: Entry)

    @Delete
    fun delete(entry: Entry)

    @Update
    fun update(entry: Entry)

    @Query("UPDATE entry SET deleteTime = :deleteTimestamp WHERE id = :entryId ")
    fun markDeleted(entryId: Int, deleteTimestamp: Long = System.currentTimeMillis())

    @Query("SELECT * FROM entry WHERE deleteTime > 0")
    fun queryDeletedLiveData(): LiveData<List<Entry>>

    @Query("SELECT * FROM entry WHERE deleteTime > 0")
    fun queryDeleted(): List<Entry>

    @Query("SELECT count(*) FROM entry a LEFT JOIN album b ON a.albumId = b.id WHERE b.type = :albumType")
    fun queryTotalCount(albumType: Int): Int

}